package com.dayuanit.dy15.atm.controller;

import com.dayuanit.dy15.atm.dto.FlowDTO;
import com.dayuanit.dy15.atm.dto.ResponseDTO;
import com.dayuanit.dy15.atm.holder.PageHolder;
import com.dayuanit.dy15.atm.service.FlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.List;

@RestController
public class FlowController extends BaseController {

    @Autowired
    private FlowService flowService;


    /**
     * 用户可以不传当前页或者每页显示的个数
     * 如果不传，我们就给个默认值。
     * 这种做法就称之为防御式的，也是兜底的。
     * 比直接报错的体验好很多。
     * @param cardId
     * @param currentPage
     * @param pageSize
     * @param request
     * @return
     */
    @RequestMapping("/flow/listFlow")
    public ResponseDTO listFlow(long cardId,
                                @RequestParam(required = false, defaultValue = "1") int currentPage,
                                @RequestParam(required = false, defaultValue = "2") int pageSize,
                                HttpServletRequest request) {
        PageHolder<List<FlowDTO>> pageHolder = flowService.listFlows(getUserId(request), cardId, currentPage, pageSize);
        return ResponseDTO.sucess(pageHolder);
    }

    @RequestMapping("/flow/loadTop10")
    public ResponseDTO loadTop10(HttpServletRequest request) {
        return ResponseDTO.sucess(flowService.listTop10(getUserId(request)));
    }

    @RequestMapping(value = "/test", produces = "application/json;charset=utf-8")
    public String test(HttpServletResponse response) {
//        response.setContentType("application/json;charset=utf-8");
        return "你好";
    }


    @RequestMapping("/flow/downLoadFlow")
    public void downLoadFlow(long cardId, HttpServletRequest request, HttpServletResponse response) {
        List<FlowDTO> flowDTOS = flowService.listCardFlow(cardId, getUserId(request));

        String fileName = "flow";

        if (!flowDTOS.isEmpty()) {
            fileName = flowDTOS.get(0).getCardNum();
        }

        response.setContentType("application/octet-stream");
        //此响应头，告诉浏览器，下载的文件名是多少
        response.addHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");

        StringBuilder sx = new StringBuilder();
        //创建表头
        sx.append("卡号").append(",")
                .append("金额").append(",")
                .append("备注").append(",")
                .append("时间").append(",").append("\n");

        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(response.getOutputStream()));
            //流水内容
            for (FlowDTO flowDTO : flowDTOS) {
                sx.append(flowDTO.getCardNum()).append(",")
                        .append(flowDTO.getAmount()).append(",")
                        .append(flowDTO.getRemark()).append(",")
                        .append(flowDTO.getCreateTime()).append("\n");
            }
            bw.write(sx.toString());
            bw.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @RequestMapping("/flow/cardFlowStatisitData")
    public ResponseDTO cardFlowStatisitData(int cardId, HttpServletRequest request) {
        return ResponseDTO.sucess(flowService.loadCardStatisticData(getUserId(request), cardId));
    }
}
